﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.IO;
using AnimEditor.Physics;

namespace AnimEditor.Tile
{

  public class TileData
  {
    public TileData(Rectangle Rect, string Name)
    {
      m_Rect = Rect;
      m_Name = Name;
    }
    public TileData()
    {
      m_Name = "";
    }

    TilePhysics m_Physics = null;
    [Browsable(false)]
    public TilePhysics Physics
    {
      get { return m_Physics; }
      set { m_Physics = value; }
    }

    int mIndex;
    [Browsable(false)]
    public int Index
    {
      get { return mIndex; }
      set { mIndex = value; }
    }

    string m_Name;
    [Category("属性"), Description("瓦片名字")]
    public string Name
    {
      get { return m_Name; }
      set { m_Name = value; }
    }

    Rectangle m_Rect = new Rectangle();
    [Category("属性"), Description("瓦片Rect")]
    public Rectangle Rect
    {
      get { return m_Rect; }
      set { m_Rect = value; }
    }

    public bool Write(BinaryWriter writer)
    {
      writer.Write(Name);
      writer.Write(Rect.X);
      writer.Write(Rect.Y);
      writer.Write(Rect.Width);
      writer.Write(Rect.Height);
      writer.Write((byte)(m_Physics!=null?1:0));
      if(m_Physics!=null)
      {
        m_Physics.Write(writer);
      }
      return true;
    }
    public bool Read(BinaryReader reader)
    {
      Name = reader.ReadString();
      Rectangle temprect = new Rectangle();
      temprect.X = reader.ReadInt32();
      temprect.Y = reader.ReadInt32();
      temprect.Width = reader.ReadInt32();
      temprect.Height = reader.ReadInt32();
      Rect = temprect;
      byte phy = reader.ReadByte();
      if(phy==1)
      {
        m_Physics = new TilePhysics();
        m_Physics.Read(reader);
      }
      return true;
    }
    public void Clear()
    {
      Rect = new Rectangle();
      Index = -1;
      m_Name = "";
      if (Physics != null)
        Physics.Clear();
      Physics = null;
    }
  }

  public class TileMgr : SingleTon<TileMgr>
  {
    List<TileData> mTile_Sprites = new List<TileData>();

    public List<TileData> Tile_Sprites
    {
      get { return mTile_Sprites; }
      set { mTile_Sprites = value; }
    }
    public bool Write(BinaryWriter writer)
    {
      int count = Tile_Sprites.Count;
      writer.Write(count);
      for (int i = 0; i < count; ++i )
      {
        Tile_Sprites[i].Index = i;
        if (!Tile_Sprites[i].Write(writer))
          return false;        
      }
      return true;
    }
    public bool Read(BinaryReader reader)
    {
      int count = reader.ReadInt32();
      for (int i = 0; i < count ; ++i )
      {
        TileData data = new TileData();
        if (!data.Read(reader))
          return false;
        Tile_Sprites.Add(data);
      }
      return true;
    }
    public void Clear()
    {
      foreach(TileData data in Tile_Sprites)
      {
        data.Clear();
      }
      Tile_Sprites.Clear();
    }
  }
}
